package com.dyzx.hbb.config;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.model.tool.ToolExecutionResult;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.ollama.management.ModelManagementOptions;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class OllamaConfig {

    @Bean
    public ToolCallingManager toolCallingManager() {
        // 使用Spring AI提供的默认实现
        return ToolCallingManager.builder().build();
    }



    @Bean
    public OllamaChatModel ollamaChatModel(
            OllamaApi ollamaApi,
            OllamaOptions options,
            ToolCallingManager toolCallingManager) {

        return OllamaChatModel.builder()
                .ollamaApi(ollamaApi)
                .defaultOptions(options)
                .toolCallingManager(toolCallingManager)
                .observationRegistry(ObservationRegistry.NOOP)
                .build();
    }



    @Bean
    public OllamaApi ollamaApi() {
        RestClient.Builder restClientBuilder = RestClient.builder()
                .messageConverters(converters -> {
                    converters.add(new MappingJackson2HttpMessageConverter() {
                        @Override
                        public boolean canRead(Class<?> clazz, MediaType mediaType) {
                            return mediaType.includes(MediaType.TEXT_PLAIN)
                                    || super.canRead(clazz, mediaType);
                        }
                    });
                });
        return new OllamaApi("http://localhost:11434", restClientBuilder, org.springframework.web.reactive.function.client.WebClient.builder());
    }

    @Bean
    public OllamaOptions ollamaOptions() {
        OllamaOptions ollamaOptions = new OllamaOptions();
        //.setModel("kimi-k2:1t-cloud").setTemperature(0.7)
        ollamaOptions.setModel("kimi-k2:1t-cloud");
     //   ollamaOptions.setTemperature(0.7);
        return ollamaOptions;
    }
}

